AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
--include('entities/base_wire_entity/init.lua')
include( 'shared.lua' )
util.PrecacheSound( "NPC_Ministrider.FireMinigun" )
util.PrecacheSound( "WeaponDissolve.Dissolve" )

function ENT:Initialize()

	self.Entity:SetModel( "models/Spacebuild/cannon1_gen.mdl" ) 
	self.Entity:SetName("Small Energy Cannon")
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )

	if WireAddon then
		self.Inputs = WireLib.CreateInputs( self, { "Fire" } )
	end

	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
		phys:EnableGravity(true)
		phys:EnableDrag(true)
		phys:EnableCollisions(true)
	end
	self.Entity:SetKeyValue("rendercolor", "255 255 255")
	self.PhysObj = self.Entity:GetPhysicsObject()
	
	--self.val1 = 0
	--RD_AddResource(self.Entity, "Munitions", 0)
	
end

function ENT:SpawnFunction( ply, tr )

	if ( !tr.Hit ) then return end
	
	local SpawnPos = tr.HitPos + tr.HitNormal * 16
	
	local ent = ents.Create( "weapon_PulseCannonSmall" )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	ent.SPL = ply
	
	return ent
	
end

function ENT:TriggerInput(iname, value)		
	if (iname == "Fire") then
		if (value > 0) then
			self.Entity:HPFire()
		end

	end
end

function ENT:PhysicsUpdate()

end

function ENT:Think()

end

function ENT:PhysicsCollide( data, physobj )
	
end

function ENT:OnTakeDamage( dmginfo )
	
end

function ENT:Use( activator, caller )

end

function ENT:HPFire()
	if (CurTime() >= self.MCDown) then
		local energy = self:GetResourceAmount("energy")
		if energy>=200 then
		self:ConsumeResource("energy", 200)
		else
			return
		end
		local Size = "ammo_pulse_shot"
		local CVel = self.Entity:GetPhysicsObject():GetVelocity():Length()
		local Pos = self.Entity:GetPos() + (self.Entity:GetUp() * 10) + (self.Entity:GetForward() * (115 + CVel))
		local Ang = self.Entity:GetForward():Angle() 
		local Vel = self.Entity:GetForward() * 1000
		self:MakeShell(CCD,Size,Pos,Ang,Vel)
		self.Entity:EmitSound("NPC_Ministrider.FireMinigun")
		self.MCDown = CurTime() + 0.4 + math.Rand(0,0.3)
		self:Recoil(500)
	end
end